package com.cob.main;

import java.applet.Applet;
import java.awt.Dimension;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;

import com.dalonedrau.engine.GameState;
import com.dalonedrau.engine.Watchable;
import com.dalonedrau.engine.Watcher;
import com.dalonedrau.opengl.GameApplet;
import com.dalonedrau.opengl.GameCanvas;
import com.dalonedrau.opengl.GameController;
import com.dalonedrau.opengl.TextureLoader;
import com.dalonedrau.util.GlobalConstants;
import com.jogamp.opengl.util.texture.Texture;

public class COBController extends GameController implements Watcher {
	/** the one and only instance of the <code>GameController</code> class. */
	private static COBController	instance;
	/**
	 * Gives access to the singleton instance of {@link GameController}.
	 * @return {@link GameController}
	 */
	public static COBController getInstance() {
		if (COBController.instance == null) {
			COBController.instance = new COBController() {};
			GameController.setInstance(COBController.instance);
			GameState.getInstance().addWatcher(COBController.instance);
		}
		return COBController.instance;
	}
	private GameCanvas		canvas;
	/** flag indicating the game has started. */
	private boolean			gameStarted		= false;
	/** flag indicating the applet has been initialized. */
	private boolean			initAppletDone	= false;
	/** the image library. */
	private TextureLoader	textureLibrary;
	/*
	 * (non-Javadoc)
	 * @see com.dalonedrau.opengl.GameController#getTexture(java.lang.String)
	 */
	@Override
	public Texture getTexture(final int id) throws Exception {
		return textureLibrary.getTexture(id);
	}
	@Override
	protected void initApplet() {
		try {
			Applet a = COBApplet.getInstance();

			a.setFocusTraversalKeysEnabled(false);
			a.setFocusTraversalKeysEnabled(false);
			a.setPreferredSize(new Dimension(
					GlobalConstants.getInstance().getScreenWidth(),
					GlobalConstants.getInstance().getScreenHeight()));
			a.setMinimumSize(new Dimension(
					GlobalConstants.getInstance().getScreenWidth(),
					GlobalConstants.getInstance().getScreenHeight()));

			// * enable double buffering
			// GLCapabilities capabilities = new GLCapabilities(null);
			GLCapabilities capabilities =
					new GLCapabilities(GLProfile.getDefault());
			capabilities.setDoubleBuffered(true);
			canvas = new GameCanvas(capabilities,
					GlobalConstants.getInstance().getScreenWidth(),
					GlobalConstants.getInstance().getScreenHeight());
			a.add(canvas);
			a.setVisible(true);
			initAppletDone = true;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/** Initializes the texture library. */
	@Override
	public final void initTextureLibrary() {
		if (textureLibrary == null) {
			try {
				textureLibrary =
						new TextureLoader("images.txt",
								"com/cob/dat/images/");
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
		}
	}
	/** Switches the game screen. */
	@Override
	public void switchScreen() throws Exception {
		if (!initAppletDone) {
			initApplet();
		}
		switch (GameState.getInstance().getCurrentState()) {
		case GameState.STARTMENU:
			Interface.getInstance().setCurrentView(0);
			break;
		}
		/*
		 * switch (GameState.getInstance().getCurrentState()) { case START_MENU:
		 * gameStarted = false; Engine.getEngine().setView(introView); break;
		 * case PARTY_CREATION: if (party.isEmpty()) {
		 * partyCreationIView.setHeroes(getRandomHeroes()); }
		 * Engine.getEngine().setView(partyCreationIView); break; case
		 * PARTY_CREATION_II:
		 * partyCreationIIView.setHeroes(getRandomInitiates());
		 * Engine.getEngine().setView(partyCreationIIView); break; case
		 * PARTY_CREATION_III: Engine.getEngine().setView(partyCreationIIIView);
		 * break; case IN_PLAY: if (!gameStarted) { gameStarted = true;
		 * party.addHeroToParty(0, getRandomHeroes()[0]); // create 1st segment
		 * DungeonMap.getInstance().addToMap( getDungeonEntry(), true, false);
		 * //startGame(); } Engine.getEngine().setView(gameView); break;
		 * default:break; }
		 */
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void watchUpdated(final Watchable data) {
		if (data instanceof GameState) {
			try {
				switchScreen();
			} catch (Exception ex) {
				COBApplet.getInstance().fatalError(ex);
			}
		}
	}
}
